#include "stdafx.h"
#include "DuiDemo.h"
#include "CVNavigatorUI.h"
#include "ListDefine.h"


const TCHAR* const kVNavigatorUIClassName = _T("VNavigatorUI");
const TCHAR* const kVNavigatorUIInterfaceName = _T("VNavigator");

const TCHAR* const kHNavigatorUIClassName = _T("HNavigatorUI");
const TCHAR* const kHNavigatorUIInterfaceName = _T("HNavigator");

CVNavigatorUI::CVNavigatorUI()
	: m_dwDelayDeltaY(0)
	, m_dwDelayNum(0)
	, m_dwDelayLeft(0)
{

}

LPCTSTR CVNavigatorUI::GetClass() const
{
	return kVNavigatorUIClassName;
}

LPVOID CVNavigatorUI::GetInterface(LPCTSTR pstrName)
{
	if (_tcsicmp(pstrName, kVNavigatorUIInterfaceName) == 0) return static_cast<CVNavigatorUI*>(this);
	return CVerticalLayoutUI::GetInterface(pstrName);
}

void CVNavigatorUI::DoEvent(TEventUI& event)
{
	if (!IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND)
	{
		if (m_pParent != NULL)
			m_pParent->DoEvent(event);
		else
			CVerticalLayoutUI::DoEvent(event);
		return;
	}

	if (event.Type == UIEVENT_TIMER && event.wParam == SCROLL_TIMERID)
	{
		if (m_dwDelayLeft > 0)
		{
			--m_dwDelayLeft;
			SIZE sz = GetScrollPos();
			LONG lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
			if ((lDeltaY > 0 && sz.cy != 0) || (lDeltaY < 0 && sz.cy != GetScrollRange().cy))
			{
				sz.cy -= (lDeltaY*2);
				SetScrollPos(sz);
				return;
			}
		}
		m_dwDelayDeltaY = 0;
		m_dwDelayNum = 0;
		m_dwDelayLeft = 0;
		m_pManager->KillTimer(this, SCROLL_TIMERID);
		return;
	}
	if (event.Type == UIEVENT_SCROLLWHEEL)
	{
		LONG lDeltaY = 0;
		if (m_dwDelayNum > 0)
			lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
		switch (LOWORD(event.wParam))
		{
		case SB_LINEUP:
			if (m_dwDelayDeltaY >= 0)
				m_dwDelayDeltaY = lDeltaY + 8;
			else
				m_dwDelayDeltaY = lDeltaY + 12;
			break;
		case SB_LINEDOWN:
			if (m_dwDelayDeltaY <= 0)
				m_dwDelayDeltaY = lDeltaY - 8;
			else
				m_dwDelayDeltaY = lDeltaY - 12;
			break;
		}

		if (m_dwDelayDeltaY > 100) m_dwDelayDeltaY = 100;
		else if (m_dwDelayDeltaY < -100) m_dwDelayDeltaY = -100;

		m_dwDelayNum = (DWORD)sqrt((double)abs(m_dwDelayDeltaY)) * 5;
		m_dwDelayLeft = m_dwDelayNum;
		m_pManager->SetTimer(this, SCROLL_TIMERID, 50U);
		return;
	}

	CVerticalLayoutUI::DoEvent(event);
}


/////////////////////////////
//CHNavigatorUI
CHNavigatorUI::CHNavigatorUI()
	: m_dwDelayDeltaY(0)
	, m_dwDelayNum(0)
	, m_dwDelayLeft(0)
{

}


LPCTSTR CHNavigatorUI::GetClass() const
{
	return kHNavigatorUIClassName;
}

LPVOID CHNavigatorUI::GetInterface(LPCTSTR pstrName)
{
	if (_tcsicmp(pstrName, kHNavigatorUIInterfaceName) == 0) return static_cast<CHNavigatorUI*>(this);
	return CHorizontalLayoutUI::GetInterface(pstrName);
}

void CHNavigatorUI::DoEvent(TEventUI& event)
{
	if (!IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND)
	{
		if (m_pParent != NULL)
			m_pParent->DoEvent(event);
		else
			CHorizontalLayoutUI::DoEvent(event);
		return;
	}

	if (event.Type == UIEVENT_TIMER && event.wParam == SCROLL_TIMERID)
	{
		if (m_dwDelayLeft > 0)
		{
			--m_dwDelayLeft;
			SIZE sz = GetScrollPos();
			LONG lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
			if ((lDeltaY > 0 && sz.cx != 0) || (lDeltaY < 0 && sz.cx != GetScrollRange().cx))
			{
				sz.cx -= (lDeltaY*2);
				SetScrollPos(sz);
				return;
			}
		}
		m_dwDelayDeltaY = 0;
		m_dwDelayNum = 0;
		m_dwDelayLeft = 0;
		m_pManager->KillTimer(this, SCROLL_TIMERID);
		return;
	}
	if (event.Type == UIEVENT_SCROLLWHEEL)
	{
		LONG lDeltaY = 0;
		if (m_dwDelayNum > 0)
			lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
		switch (LOWORD(event.wParam))
		{
		case SB_LINELEFT:
			if (m_dwDelayDeltaY >= 0)
				m_dwDelayDeltaY = lDeltaY + 8;
			else
				m_dwDelayDeltaY = lDeltaY + 12;
			break;
		case SB_LINERIGHT:
			if (m_dwDelayDeltaY <= 0)
				m_dwDelayDeltaY = lDeltaY - 8;
			else
				m_dwDelayDeltaY = lDeltaY - 12;
			break;
		}
		if
			(m_dwDelayDeltaY > 100) m_dwDelayDeltaY = 100;
		else if
			(m_dwDelayDeltaY < -100) m_dwDelayDeltaY = -100;

		m_dwDelayNum = (DWORD)sqrt((double)abs(m_dwDelayDeltaY)) * 5;
		m_dwDelayLeft = m_dwDelayNum;
		m_pManager->SetTimer(this, SCROLL_TIMERID, 50U);
		return;
	}

	CHorizontalLayoutUI::DoEvent(event);
}
